#include "mainwindow.h"
#include <QGraphicsView>
#include <QStackedWidget>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent):QMainWindow(parent){
    mainScreenWidget = new MainScreenWidget(this);
    gameSettingsWidget = new GameSettingsWidget(this);
    pickGameModeWidget = new PickGameModeWidget(this);    
    loginWidget = new LoginWidget(this);
    challengeRoom = new ChallengeRoom(this);
    networkModule = new NetworkModule("http://localhost",8040,this);

    gameWidget = new GameWidget(gameSettingsWidget,this,networkModule);
    stackedWidget = new QStackedWidget(this);

    stackedWidget->addWidget(mainScreenWidget);
    stackedWidget->addWidget(gameSettingsWidget);
    stackedWidget->addWidget(pickGameModeWidget);
    stackedWidget->addWidget(loginWidget);
    stackedWidget->addWidget(challengeRoom);
    stackedWidget->addWidget(gameWidget);

    setMinimumSize(1024,640);
    setSizePolicy(QSizePolicy::MinimumExpanding,QSizePolicy::MinimumExpanding);
    setStyleSheet(QString::fromUtf8("QMainWindow{\nbackground-color:#888c7e;\n}"));

    connect(mainScreenWidget,SIGNAL(settingsSelected()),this,SLOT(toSettings()));
    connect(mainScreenWidget,SIGNAL(quitSelected()),this,SLOT(close()));
    connect(gameSettingsWidget,SIGNAL(okClicked()),this,SLOT(toMainWidget()));
    connect(mainScreenWidget,SIGNAL(playSelected()),this,SLOT(toPickGameMode()));
    connect(pickGameModeWidget,SIGNAL(backClicked()),this,SLOT(toMainWidget()));
    connect(pickGameModeWidget,SIGNAL(singleplayerSelected()),this,SLOT(toGameWidgetSingle()));

    connect(loginWidget,SIGNAL(back()),this,SLOT(toPickGameMode()));
    connect(pickGameModeWidget,SIGNAL(multiplayerSelected()),this,SLOT(toLoginWidget()));

    connect(loginWidget,SIGNAL(login(QString,QString)),networkModule,SLOT(attemptLogin(QString,QString)));
    connect(networkModule,SIGNAL(loginFailure()),loginWidget,SLOT(displayLoginError()));
    connect(networkModule,SIGNAL(loginSuccess()),this,SLOT(toChallengeRoomWidget()));

    connect(networkModule,SIGNAL(connectionError()),loginWidget,SLOT(displayConnectionError()));
    connect(networkModule,SIGNAL(connectionError()),this,SLOT(handleConnectionError()));
    connect(networkModule,SIGNAL(playersAvailableForChallenge(QList<QString>)),challengeRoom,SLOT(updatePlayerList(QList<QString>)));

    connect(networkModule,SIGNAL(logoutSuccess()),this,SLOT(toPickGameMode()));
    connect(challengeRoom,SIGNAL(logout()),networkModule,SLOT(logoutRequest()));

    connect(networkModule,SIGNAL(pendingChallengeFrom(QString)),challengeRoom,SLOT(challengedBy(QString)));
    connect(networkModule,SIGNAL(noPendingChallenges()),challengeRoom,SLOT(handleNoChallenges()));

    connect(challengeRoom,SIGNAL(challengeByPlayerAccepted(QString)),networkModule,SLOT(acceptChallengeBy(QString)));
    connect(challengeRoom,SIGNAL(challengeByPlayerDeclined(QString)),networkModule,SLOT(declineChallengeBy(QString)));

    connect(challengeRoom,SIGNAL(challengePlayer(QString)),networkModule,SLOT(challengePlayer(QString)));
    connect(challengeRoom,SIGNAL(cancelChallenge()),networkModule,SLOT(cancelActiveChallengeRequest()));
    connect(networkModule,SIGNAL(challengeDeliveredToPlayer(QString)),challengeRoom,SLOT(challengeDelivered(QString)));
    connect(networkModule,SIGNAL(challengeDeliveryToPlayerFailed(QString)),challengeRoom,SLOT(handleChallengeDeliveryFailure(QString)));

    connect(networkModule,SIGNAL(challengeDeclined()),challengeRoom,SLOT(handleChallengeDeclined()));

    connect(networkModule,SIGNAL(readyForGameState()),this,SLOT(toGameWidgetMultiplayer()));

    setCentralWidget(stackedWidget);
}

void MainWindow::handleConnectionError(){
    if(stackedWidget->currentWidget() != loginWidget){
        challengeRoom->closeAll();

        QMessageBox messageBox;
        messageBox.setIcon(QMessageBox::Information);
        messageBox.setWindowTitle("Connection Error");
        messageBox.setText("<h2>We are terribly sorry, but a connection error has occurred!</h2>");
        messageBox.setStandardButtons(QMessageBox::Ok);

        messageBox.setStyleSheet(QString::fromUtf8("QMessageBox{\nbackground-color:#888c7e;\n}\nQLabel{\ncolor:#fff;\n}\nQPushButton{\npadding:10px 20px 10px 20px;\nborder:3px solid #78846a;\nbackground-color:#a5b592;\ncolor:#fff;\n}"));
        messageBox.exec();

        toPickGameMode();
    }
}

void MainWindow::toMainWidget(){
    stackedWidget->setCurrentWidget(mainScreenWidget);
}

void MainWindow::toSettings(){
    stackedWidget->setCurrentWidget(gameSettingsWidget);
}

void MainWindow::toPickGameMode(){
    stackedWidget->setCurrentWidget(pickGameModeWidget);
}

void MainWindow::toGameWidgetMultiplayer(){
    challengeRoom->closeAll();
    stackedWidget->setCurrentWidget(gameWidget);

    gameWidget->startGameMultiplayer();
    connect(gameWidget,SIGNAL(backClicked()),this,SLOT(toChallengeRoomWidget()));
}

void MainWindow::toGameWidgetSingle(){
    gameWidget->switchToSingleGameVersion();
    stackedWidget->setCurrentWidget(gameWidget);

    connect(gameWidget,SIGNAL(backClicked()),this,SLOT(toPickGameMode()));
}

void MainWindow::toLoginWidget(){
    loginWidget->clearState();
    stackedWidget->setCurrentWidget(loginWidget);
}

void MainWindow::toChallengeRoomWidget(){
    networkModule->startRequestLoop();
    stackedWidget->setCurrentWidget(challengeRoom);

}

MainWindow::~MainWindow(){
}
